package com.sky.controller.admin;


import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用相关接口")
@Slf4j
public class CommonController {

    @Resource
    private AliOssUtil aliOssUtil;

    @ApiOperation("文件上传接口")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        log.info("文件上传接口：{}",file);


        //获取原始文件名
        String originalFilename = file.getOriginalFilename();//xxx.jpg

        //获取后缀
        String extname = originalFilename.substring(originalFilename.lastIndexOf("."));
        //设置上传文件名=UUID+原始文件名后缀
        String fileName = UUID.randomUUID().toString().replace("-","")+extname;

        //调用工具实现文件上传
        String path = aliOssUtil.upload(file.getBytes(), fileName);

        //返回
        return Result.success(path);
    }
}
